import 'package:get/get.dart';
import 'package:safetymonitor/app/components/toast_widget.dart';
import 'package:safetymonitor/app/core/http/http.dart';
import 'package:safetymonitor/app/data/common/choice_bean.dart';

class CommonSelectionController<T extends ChoiceBean> extends GetxController {
  Map<String, dynamic> queryParameters = {}; // 请求参数
  bool isRadio = true; // 是否单选
  List<T> selectedList = []; // 选中的列表
  String interfaceUrl = ''; // 接口地址
  String searchName = 'name'; // 搜索字段名称
  String type = Get.arguments['type'] as String? ?? 'get';

  /// 解析传参
  Future<void> parsingParameters() async {
    final Map<String, dynamic> arguments = Get.arguments as Map<String, dynamic>;
    // 请求参数是否为空
    if (arguments['queryParameters'] != null && arguments['queryParameters'] is Map<String, dynamic>) {
      final Map<String, dynamic> query = arguments['queryParameters'] as Map<String, dynamic>;
      for (var key in query.keys) {
        //获取请求参数
        queryParameters[key] = query[key];
      }
    }
    // 接口地址是否为空
    if (arguments['interfaceUrl'] != null && arguments['interfaceUrl'] is String) {
      interfaceUrl = arguments['interfaceUrl'] as String;
    }
    // 搜索字段名称是否为空
    if (arguments['searchName'] != null && arguments['searchName'] is String) {
      searchName = arguments['searchName'] as String;
    }
    // 是否单选
    if (arguments['isRadio'] != null && arguments['isRadio'] is bool) {
      isRadio = arguments['isRadio'] as bool;
    }
    // 请求类型
    if (arguments['type'] != null && arguments['type'] is String) {
      type = arguments['type'] as String;
    }
  }

  /// 获取列表数据
  Future<List<T>> getDate({
    required int pageNum,
    required int pageSize,
    required bool enableLoad,
    String? search,
  }) async {
    selectedList.clear();
    await parsingParameters();
    if (enableLoad) {
      queryParameters['pageNum'] = pageNum;
      queryParameters['pageSize'] = pageSize;
    }
    queryParameters[searchName] = search;
    // 调用接口
    final res = await HttpUtil().generateGetRequest<List<T>>(interfaceUrl, queryParameters: queryParameters).execute();

    List<T>? data;
    res.onSuccess((result) {
      data = result.data;
    });
    return data ?? [];
  }

  /// 获取Post数据列表
  Future<List<T>> getPostData({
    required int pageNum,
    required int pageSize,
    required bool enableLoad,
    String? search,
  }) async {
    selectedList.clear();
    await parsingParameters();
    if (enableLoad) {
      queryParameters['pageNum'] = pageNum;
      queryParameters['pageSize'] = pageSize;
    }
    queryParameters[searchName] = search;
    // 调用接口
    final res = await HttpUtil().generatePostRequest<List<T>>(interfaceUrl, data: queryParameters).execute();

    List<T>? data;
    res.onSuccess((result) {
      data = result.data;
    });
    return data ?? [];
  }

  /// 选择点击事件
  void onTap(T item) {
    //如果是单选
    if (isRadio) {
      for (var selectItem in selectedList) {
        //清空之前选中的数据
        selectItem.setIsChecked(false);
      }
      item.setIsChecked(true);
      selectedList.clear();
      selectedList.add(item);
    } else {
      //判断点击前的状态
      final bool isChecked = item.getIsChecked();
      if (isChecked) {
        //如果之前是选中的，就修改状态后移出
        item.setIsChecked(false);
        selectedList.remove(item);
      } else {
        //如果之前是未选中的，就修改状态后添加
        item.setIsChecked(true);
        selectedList.add(item);
      }
    }
    update();
  }

  ///完成
  void complete() {
    if (selectedList.isEmpty) {
      AppToast.show('请选择');
      return;
    }
    Get.back(result: selectedList);
  }
}
